<script type="text/javascript">
    var flightResult; //RESULT FOUND
    var response; //RESULT FOUND
    var flightLength;
    var sortIndex = 0;
    var isContinue = 0;
    var flightResultTemp = new Array();
    
	
    function getResult(id, taskId) {
        controller_url = "${pageContext.request.contextPath}/task/result.html";

        var json = {
            "taskId": taskId
        };
        var notyId = "\"" + id + "\"";
        var closeId = "\"" + "close-"+id+ "\"";
//         var lastPostLoaderId = "\"" + "lastPostsLoader-"+taskId+ "\"";
      	//show loading panel in jsloading.jsp
        showNativeLoading(id);
        // click and show
//         	$('div.row.bg-expand-list').not($('[id=' + notyId + ']')).hide();
//         	$('[id=' + lastPostLoaderId + ']').html('');
        	$('[id=' + notyId + ']').slideDown(
                function () {
                    // ajax
                    $.ajax({
		                url: controller_url,
		                data: JSON.stringify(json),
		                type: 'POST',
		                dataType: "text",
		                beforeSend: function (xhr) {
		                    xhr.setRequestHeader("Accept",
		                        "application/json");
		                    xhr.setRequestHeader(
		                        "Content-Type",
		                        "application/json");
		                },
		                success: function (jsonFlights) {
		                    // load result into #lasPostLoader
		                    //e.preventDefault();
		                    // append
		                    unShowNativeLoading(id);
		                    flightResult = JSON.parse(jsonFlights);
		                    flightLength = flightResult.length;
		                    if(flightLength === 0){
		                        message = "There is no result for this task.";
		                        generate('warning', message);
		                        $('[id=' + notyId + ']').slideUp();
		                    }
		                    else{
		                    	$('[id=' + notyId + ']').css({
				                        "max-height": "300px"
				                    });
			                    loadData(id);
			                    $('[id=' + closeId + ']').show();
		                    }
		                },
		                error: function () {
		                	unShowNativeLoading(id);
		                    message = "There is no result for this task.";
		                    generate('error', message);
		                }
		            });
                    // end ajax
                }
            );
		// end show
    }
    // close result list
    function closeResult(taskId) {
    	var notyId = "\"" + taskId + "\"";
    	var closeId = "\"" + "close-"+taskId+ "\"";
    	var openId = "\"" + "open-"+taskId+ "\"";
    	$('[id=' + notyId + ']').slideUp();
    	$('[id=' + closeId + ']').hide();
    	$('[id=' + openId + ']').show();
	}
    //open result list
    function openResult(taskId) {
    	var notyId = "\"" + taskId + "\"";
    	var closeId = "\"" + "close-"+taskId+ "\"";
    	var openId = "\"" + "open-"+taskId+ "\"";
    	$('[id=' + notyId + ']').slideDown();
    	$('[id=' + closeId + ']').show();
    	$('[id=' + openId + ']').hide();
    }
    //ajax request function
    //con mu Luong !
    function getProviderLink(label){
		var str=label+'';
		var i=str.indexOf('.', 0);
		if(i==-1){
			return 'http://www.google.com/search?q='+label;
		}
		return 'http://'+label;
	}
  //loadData flight result
    function loadData(taskId) {
        var contextpath = '${pageContext.request.contextPath}';
        var index = 0;
        var taskIdFn = "\"" + "lastPostsLoader-"+taskId+ "\"";
        $.each(
                flightResult,
                function (index, flight) {
                    var divOpen = '<div class="row divFlightResult">' + '<div class="columns">' + '<div class="large-16 medium-16 small-16 columns flight-detail-top">';
                    var divClose = '</div>' + '</div>' + '</div>';
                    var numStops;
                    var classType;
                    var overOB;
                    //Outbound Info
                    if (flight.outBound.numberStops > 0) {
                        numStops = flight.outBound.numberStops + ' stop(s) ' + flight.outBound.airportsExpanded + ' ';
                    } else {
                        numStops = 'Direct  ';
                    }

                    if (parseInt(flight.outBound.dayOver, 10) > 0) {
                        overOB = "(+" + flight.outBound.dayOver + ")";
                    } else {
                        overOB = "";
                    }
                    classType = (flight.outBound.departAirports[0].airline.flightClass == '0') ? 'Economy' : 'Business';
                    var n = flight.outBound.arriveAirports.length;
                    var divOutbound = '<div class="large-6 medium-6 small-6 columns un-padding">' + '<div class="row collapse">' + '<div class="large-2 medium-2 small-4 columns">' + '<img src="' + contextpath + '/resources/img/ic_flight_depart.png" class="flight-icon">' + '</div>' +

                    '<div class="large-14 medium-14 small-12 columns un-padding">' + '<span class="out-bound-airline">' + flight.outBound.departAirports[0].airline.name + '</span> <br> <span class="gray">' + flight.outBound.departAirports[0].departureTimeYYYYMMDD + '</span><br>' + '<div style="margin-top: 5px;">' + '<span class="green-bold">' + flight.outBound.departAirports[0].code + " " + '</span>' + '<span class="navy">' + flight.outBound.departAirports[0].departureTimeByHHmm + '</span>' + '- <span class="green-bold">' + flight.outBound.arriveAirports[n - 1].code + "&nbsp" + '</span>' + '<span class="navy">' + flight.outBound.arriveAirports[n - 1].arrivalTimeByHHmm + "&nbsp" + overOB + '</span>' + '<br> <span class="green-bold">' + numStops + '</span>' + '<span class="navy">' + flight.outBound.totalDurationHHMM + '</span>' + '<br> <span class="gray">' + classType + '</span>' + '</div>' + '</div>' + '</div>' + '</div>';
                    // Price
                    var divPrice = '<div class="large-4 medium-4 columns un-padding">' 
                    + '<span class="price"> ' 
                    + '<font color="red" size="3" id="price">' 
                    + flight.price.amount + '</font>' 
                    + '<input type="hidden" value=' 
                    + flight.price.amount + '>' + ' ' 
                    + '<font name="vrowCode" id="currencyCode">' 
                    + flight.price.currencyCode + '</font>' 
                    + ' </span><br>' 
                    + '<span class="price-source"><a target="_blank" href="www.axonactive.vn' 
                    + '">www.axonactive.vn'
					+ '</a></span>' + '</div>';
                    var finalResult = divOpen + divOutbound + divPrice + divClose;

                    // Inbound Info
                    if (flight.roundTrip === true) {
                        var overIB;
                        if (flight.inBound.numberStops > 0) {
                            numStops = flight.inBound.numberStops + ' stop(s) ' + flight.inBound.airportsExpanded + ' ';
                        } else {
                            numStops = 'Direct  ';
                        }
                        if (flight.inBound.dayOver > 0) {
                            overIB = "(+" + flight.inBound.dayOver + ")";
                        } else {
                            overIB = "";
                        }
                        classType = (flight.inBound.departAirports[0].airline.flightClass == '0') ? 'Economy' : 'Business';
                        n = flight.inBound.arriveAirports.length;
                        var divInbound = '<div class="large-6 medium-6 small-6 columns un-padding">' + '<div class="row collapse">' + '<div class="large-2 medium-2 small-4 columns">' + '<img src="' + contextpath + '/resources/img/ic_flight_return.png" class="flight-icon">' + '</div>' +

                        '<div class="large-14 medium-14 small-12 columns un-padding">' + '<span class="in-bound-airline">' + flight.inBound.departAirports[0].airline.name + '</span> <br> <span class="gray">' + flight.inBound.departAirports[0].departureTimeYYYYMMDD + '</span><br>' + '<div style="margin-top: 5px;">' + '<span class="green-bold">' + flight.inBound.departAirports[0].code + "&nbsp" + '</span>' + '<span class="navy">' + flight.inBound.departAirports[0].departureTimeByHHmm + '</span>' + '- <span class="green-bold">' + flight.inBound.arriveAirports[n - 1].code + "&nbsp" + '</span>' + '<span class="navy">' + flight.inBound.arriveAirports[n - 1].arrivalTimeByHHmm + "&nbsp" + overIB + '</span>' + '<br> <span class="green-bold">' + numStops + '</span>' + '<span class="navy">' + flight.inBound.totalDurationHHMM + '</span>' + '<br> <span class="gray">' + classType + '</span>' + '</div>' + '</div>' + '</div>' + '</div>';
                        finalResult = divOpen + divOutbound + divInbound + divPrice + divClose;
                    }
            		$('[id=' + taskIdFn + ']').append(finalResult);
                });
    }
</script>